package interfazeak;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import db.BezeroKud;
import db.BiltegiaKud;
import db.EnboteilaratuKud;

public class Ordaindu extends JDialog {
	private Bezeroa guraso;
	private final JPanel contentPanel = new JPanel();
	private JLabel kobratulbl;
	private JTextField kutxaktxt;
	private JButton gorde;
	private JLabel bezerolbl;
	private JLabel zorraklbl;
	
	public static void main(String[] args) {
	try {
		final Boolean MODALA = true;
		int i=0;
		String izena = null;
		Ordaindu dialog = new Ordaindu(new Bezeroa(null), MODALA,i, izena);
		dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		dialog.setVisible(true);
	} catch (Exception e) {
		e.printStackTrace();
	}
}
	
	public Ordaindu(Bezeroa guraso, boolean b, final int id, String izena) {
		
		this.guraso = guraso;
		
		int i = BezeroKud.lortukutxakguztira(id);
		int kobratu = BezeroKud.lortuordainduta(id);
		final int zorrak= i-kobratu;
		
		setBounds(100, 100, 300, 350);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		setTitle("KOBRATU");
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
	
		bezerolbl = new JLabel(izena);
		bezerolbl.setBounds(50, 40, 160, 25);
		contentPanel.add(bezerolbl);
		
		zorraklbl = new JLabel("KUTXAK ZORRETAN: "+zorrak+" ");
		zorraklbl.setBounds(50, 80, 200, 25);
		contentPanel.add(zorraklbl);
		
				
		kobratulbl = new JLabel("KOBRATUTAKO KUTXAK ");
		kobratulbl.setBounds(50, 120, 200, 25);
		contentPanel.add(kobratulbl);
		
		kutxaktxt = new JTextField();
		kutxaktxt.setBounds(90, 160,70, 30);
		contentPanel.add(kutxaktxt);
		
		gorde = new JButton("GORDE");
		gorde.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				int kutxak = 0, bete =0;
				
				try {
					String katea = kutxaktxt.getText();
					kutxak = Integer.parseInt(katea);
					
				}
				catch (NumberFormatException e){
					bete =1;
				}
				if (bete ==1 ){
					JOptionPane.showMessageDialog(Ordaindu.this,
							"Kutxa kopurua sartu, ZENBAKIA","KONTUZ",
						    JOptionPane.WARNING_MESSAGE);
				}
				else if (kutxak>zorrak) {
					JOptionPane.showMessageDialog(Ordaindu.this,
							"Ezinezkoa da kopuru hori kobratzea","KONTUZ",
						    JOptionPane.WARNING_MESSAGE);
				}
				else {
				
					int konfirmatu = JOptionPane.showConfirmDialog(
							Ordaindu.this, "Gorde nahi duzu?");
				if (JOptionPane.OK_OPTION == konfirmatu) {
				Ordaindu.this.kobratu(id,kutxak);
				Ordaindu.this.dispose();
				Ordaindu.this.guraso.ezabatutaula();
				Ordaindu.this.guraso.bistaratu();
				}
					
				else if (JOptionPane.NO_OPTION==konfirmatu){
					Ordaindu.this.dispose();
				}
				}
			}
		});
		gorde.setBounds(70,250, 120, 30);
		contentPanel.add(gorde);
		
	}

	protected void kobratu(int id, int kutxak) {
		
		BezeroKud.eguneratuOrdainduak(id,kutxak);
	}

}
